package user.controllers;

import helpers.IdUniqueHelper;
import helpers.LanguageHelper;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import search.ArticleSearchEngine;
import user.forms.ArticleForm;
import user.models.ArticleModel;
import user.models.CategoryModel;

import entities.ApplicationConfig;
import entities.Article;
import entities.ArticleSearch;
import entities.Category;
import entities.User;

public class ArticleEditServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		ApplicationConfig appConfig = (ApplicationConfig) request.getAttribute("sysConfig");
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String param = URLDecoder.decode(path.replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		String[] params = param.split("/");
		
		String id = params[0];
		String authKey = "";
		if (params.length > 1) {
			authKey = params[1];
		}
		
		ArticleModel articleModel = ArticleModel.getInstance();
		Article article = (Article) articleModel.getArticle(appConfig.getApplicationId(), id);
		
		if (article == null || !article.getAuthKey().equals(authKey)) {
			response.sendRedirect("/msg/page-not-found");
			return;
		}
		
		TreeMap check = new TreeMap();
		
		String language = (String) request.getAttribute("language");
		if (language == null || language.isEmpty()) {
			language = "en";
		}
		
		CategoryModel categoryModel = CategoryModel.getInstance();
		List<Category> categories = categoryModel.getAll(appConfig.getApplicationId());
		
		//Get User
		User user = (User) request.getAttribute("user_logged_in");
		
		request.setAttribute("check", check);
		request.setAttribute("article", article);
		request.setAttribute("categories", categories);
		request.setAttribute("user", user);
		try {
			request.getRequestDispatcher("/user/pages/article_form.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		ApplicationConfig appConfig = (ApplicationConfig) request.getAttribute("sysConfig");
		
		String language = (String) request.getAttribute("language");
		if (language == null || language.isEmpty()) {
			language = "en";
		}
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String param = URLDecoder.decode(path.replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		String[] params = param.split("/");
		
		String id = params[0];
		String authKey = "";
		if (params.length > 1) {
			authKey = params[1];
		}
		
		CategoryModel categoryModel = CategoryModel.getInstance();
		List<Category> categories = categoryModel.getAll(appConfig.getApplicationId());
		
		ArticleModel articleModel = ArticleModel.getInstance();
		Article article = articleModel.getArticle(appConfig.getApplicationId(), id);
		
		User user = (User) request.getAttribute("user_logged_in");
		if (user != null) {
			article.setCreator(user.getId());
		}
		
		TreeMap form = ArticleForm.checkValid(request, article);
		TreeMap check = (TreeMap) form.get("checkValid");
		article = (Article) form.get("article");
		if (check.size() == 0) {
			articleModel.insert(appConfig.getApplicationId(), article);
			
			//Update tag
			//RecentlyTag.insert(article.getKeywords());
			
			request.setAttribute("success", "Edit article success with id" + article.getId());
		}
		
		request.setAttribute("check", check);
		request.setAttribute("article", article);
		request.setAttribute("categories", categories);
		request.setAttribute("user", user);
		try {
			request.getRequestDispatcher("/user/pages/article_form.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
}
